<template>
  <form class="form form-login" method="post">
    <div class="form-group">
      <div class="form-label text-left"><label>用户名</label></div>
      <div class="form-wall form-grep">
        <div class="form-field"><input required class="form-input" type="text" name="username" v-model="username" /></div>
      </div>
    </div>
    <div class="form-group">
      <div class="form-label text-left"><label>密码</label></div>
      <div class="form-wall form-grep">
        <div class="form-field">
          <input
            name="password"
            class="form-input" type="password"
            v-enter="login"
            v-model="password"
          />
        </div>
      </div>
    </div>
  </form>
</template>

<script>
/* 登录窗口 */
import auth from '../../service/action/auth.action.js'
import messager from '../../components/messager/messager.js'
import eventBus from '../../components/event-bus.js'

export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },

  methods: {
    /**
     * @function 登录
    */
    login () {
      return auth.login({
        username: this.username,
        password: this.password
      }).then(result => {
        let data = result.data || {}
        if (data.token) {
          // 保存token
          localStorage.setItem('dog-token', data.token)
          messager.success('登录成功')
          // 关闭窗口
          eventBus.$emit('onLoginSuccess')
          return true
        }
        else {
          messager.error('登录失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">

</style>
